<script>
import mysocket from './socket.js';
export default {
	globalData: {
		/**"YL-8系","YL-6系","YL-4系"*/
		type: 0
	},
	//打开应用时触发
	onLoad: function() {
		//res返回网络是否连接的数据
		uni.onNetworkStatusChange(function(res) {
			//这里是断网和连接网络才会触发
		    console.log('网络类型:' + res.networkType + ',网络连接:' + res.isConnected);
		    this.connectSocket(false, false, true);
		   });   
	},
	//onLaunch是第一次打开应用触发
	onLaunch: function() {
		this.socketing();
		// #ifdef APP-PLUS
		plus.screen.lockOrientation('landscape-primary'); //锁定
		// #endif
		// console.log('第一次打开应用')
	},
	//onshow 是打开应用触发
	onShow: function() {
		// #ifdef APP-PLUS
		plus.navigator.setFullscreen(true); //去除系统导航
		// #endif
		this.connectSocket(false, false, true);
		// console.log('打开应用')
	},
	
	//熄屏或者把应用放到后台触发
	onHide: function() {
		console.log('App Hide');
	},
	methods: {
		//连接socket
		socketing() {
			let socketDataController = {
				is_globalsocket: false, //是否全局
				client_data: {}, //链接标识
				socketMsg: false, //收到的信息
				connected: false, //收到的信息
				connecting: false, //收到的信息
				socketTask: false,
				timer: null,
				isFiristConnected: true //是否是app第一次连接
			};
			uni.setStorageSync('socketDataController', socketDataController);
			//是否全局websocket
			// mysocket.isglobalsocket();
			this.connectSocket(false, false, true);
		},
		//连接socket方法
		connectSocket(a, b, c) {
			// if(!uni.getStorageSync("TK")){
			// 	return
			// }
			console.log('已执行1');
			let socketDataController = uni.getStorageSync('socketDataController');

			socketDataController.isFiristConnected = c; //记录是否第一次连接
			if (a == false && b == false) {
				socketDataController.connected = a;
				socketDataController.connecting = b;
			}
			
			if (socketDataController.connected || socketDataController.connecting) {
				// uni.showModal({
				// 	content: '正在连接或者已经连接，请勿重复连接',
				// 	showCancel: false
				// });
				return false;
			}
			socketDataController.connecting = true;
			uni.setStorageSync('socketDataController', socketDataController);
			uni.connectSocket({
				// url: 'ws://10.20.52.236:9530/Api/ws',
				// url: 'ws://10.20.51.153:9530/Api/ws',
				url: 'ws://10.20.54.37:9530/Api/ws',
				// #ifdef MP
				header: {
					'content-type': 'application/json'
				},
				// #endif
				// #ifdef MP-WEIXIN
				method: 'GET',
				// #endif
				success(res) {
					console.log('这里是接口调用成功的回调，不是连接成功的回调，请注意');
					// 这里是接口调用成功的回调，不是连接成功的回调，请注意
				},
				fail(err) {
					// 这里是接口调用失败的回调，不是连接失败的回调，请注意
					//这里是接口调用失败的回调，不是接失败的回调，请注意
				}
			});
			uni.onSocketOpen(function(res) {
				//监听WebSocket连接打开事件。
				socketDataController.connecting = false;
				socketDataController.connected = true;
				console.log('WebSocket连接已打开！');
				// clearInterval(timer)
				socketDataController.timer = null;
				//心跳发送
				socketDataController.timer = setInterval(() => {
					uni.sendSocketMessage({
						data: '{"type":"ping"}'
					});
					console.log('心跳中...');
				}, 40000);

				uni.setStorageSync('socketDataController', socketDataController);
			});

			uni.onSocketError(function(res) {
				//监听WebSocket错误。
				socketDataController.connecting = false;
				socketDataController.connected = false;
				uni.setStorageSync('isConnected', false);
				uni.hideLoading();
				console.log('连接失败，可能是websocket服务不可用，请稍后再试', res);
				uni.setStorageSync('socketDataController', socketDataController);
			});

			uni.onSocketMessage(function(res) {
				//监听WebSocket接受到服务器的消息事件。
				console.log('接收消息', res);
				return;
				let mesData = JSON.parse(res.data);
				if (mesData.msg_type == 'init') {
					// client_data = mesData;
					// alckSocket.bandNotice(mesData.client_id)
				} else {
					console.log('接收消息', mesData);
					uni.$emit('update', JSON.stringify(mesData));
					// #ifdef APP-PLUS
					uni.vibrateLong({
						success: function() {
							console.log('success');
						}
					});
					// #endif
				}
			});

			uni.onSocketClose(function(res) {
				//监听WebSocket关闭。
				socketDataController.connected = false;
				socketDataController.socketMsg = false;
				uni.setStorageSync('isConnected', false);
				clearInterval(socketDataController.timer);
				console.log('链接关闭', res);
				uni.closeSocket();
				uni.setStorageSync('socketDataController', socketDataController);
			});
		}
	}
};
</script>

<style>
/*每个页面公共css */
page {
	color: #fff;
	font-family: PingFangSC-Medium;
}

    .uni-picker-view-mask {

        background: #00000000 !important;

    }
    .uni-picker-view-mask .none{
    
        background: #00000000 !important;
    
    }

.input::-webkit-input-placeholder {
	font-size: 4vh;
}
.m-b-1{
		margin-bottom: 1vh;
	}
.m-b-2{
		margin-bottom: 2vh;
	}
.m-b-3{
		margin-bottom: 3vh;
	}
.container {
	height: 100%;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: space-between;
	padding: 200rpx 0;
	box-sizing: border-box;
}

.fw400 {
	font-weight: 400;
}

.fw500 {
	font-weight: 500;
}

.overhide {
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.txtOver {
	display: -webkit-box;
	overflow: hidden;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 1;
}

.fz10 {
	font-size: 10px;
}

.fz12 {
	font-size: 12px;
}

.fz14 {
	font-size: 14px;
}

.fz16 {
	/* font-size: 16px; */
	font-size: 1vw;
}

.fz18 {
	font-size: 18px;
}

.fz20 {
	font-size: 20px;
}

.fz22 {
	font-size: 22px;
}

.fz24 {
	font-size: 24px;
}

.rem1 {
	font-size: 7vh;
	/* font-size: 1rem; */
}

.rem03 {
	font-size: 2.5vh;
}

.rem02 {
	font-size: 20rpx;
	/* font-size: 0.2rem; */
}

.rem04 {
	/* font-size: 0.4rem; */
	font-size: 3vh;
}

.rem06 {
	font-size: 3.5vh;
}

.rem08 {
	font-size: 4vw;
}

.rem15 {
	font-size: 12vh;
	/* font-size: 5vw; */
	/* font-size: 1.5rem; */
}

.bold {
	font-weight: bold;
}

.line_height10 {
	line-height: 10px;
}

.line_height17 {
	line-height: 17px;
}

.line_height20 {
	line-height: 20px;
}

.margin_left1 {
	margin-left: 1px;
}

.margin_left5 {
	margin-left: 5px;
}

.margin_left10 {
	margin-left: 10px;
}

.margin_left14 {
	margin-left: 14px;
}

.margin_left20 {
	margin-left: 20px;
}

.margin_top1 {
	margin-top: 1px;
}

.margin_top2 {
	margin-top: 2px;
}

.margin_top5 {
	margin-top: 5px;
}

.margin_top10 {
	margin-top: 10px;
}

.margin_top14 {
	margin-top: 14px;
}

.flex_row_between {
	display: flex;
	flex-direction: row;
	justify-content: space-between;
}

.flex_col_between {
	display: flex;
	flex-direction: column;
	justify-content: space-between;
}

.flex_row {
	display: flex;
	flex-direction: row;
}

.flex_col {
	display: flex;
	flex-direction: column;
}

.flex_wrap {
	flex-wrap: wrap;
}

.allCenter {
	display: flex;
	align-items: center;
	justify-content: center;
}

.alignCenter {
	display: flex;
	align-items: center;
}

.flex_column {
	display: flex;
	flex-direction: column;
}
/* 主要内容背景 */
.main-pop-con{
    overflow: hidden;
    border-radius: 1vh;
    background-color: #4A5079;
}

.between_padding {
	padding: 0px 14px 0px 14px;
}

.box_padding {
	padding: 10px 14px 10px 14px;
}

.box_margin {
	margin: 10px 14px 0px 14px !important;
}

.img_px10 {
	width: 10px;
	height: 10px;
}

.img_px14 {
	width: 14px;
	height: 14px;
}

.img_px18 {
	width: 18px;
	height: 18px;
}

.img_px60 {
	width: 8vh;
	height: 8vh;
}

.border_r5 {
	border-radius: 5px;
}

.border_r10 {
	border-radius: 10px;
}

.border5 {
	border-bottom-style: solid;
	border-width: 0.5px;
	border-color: #383e62;
}

.backColorFBFBFB {
	background-color: #fbfbfb;
}

.backColorFFF {
	background-color: #fff;
}

.backColorF6F6F6 {
	background-color: #f6f6f6;
}

.color868686 {
	color: #868686;
}

.color000 {
	color: #000;
}

.colorFFF {
	color: #fff;
}

::-webkit-scrollbar {
	display: none;
	width: 0;
	height: 0;
	color: transparent;
}
</style>
